package com.iflytek.universitymanagement.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iflytek.universitymanagement.config.Result;
import com.iflytek.universitymanagement.domain.StudentScore;
import com.iflytek.universitymanagement.service.StudentScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/studentScore")
public class StudentScoreController {

    @Autowired
    StudentScoreService studentScoreService;


    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        StudentScore studentScore = studentScoreService.getById(id);
        return Result.success(studentScore);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<StudentScore> list = studentScoreService.list();
        return Result.success(list);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Map<String, String> addStudentScore(@RequestBody StudentScore studentScore) {
        boolean result = studentScoreService.addStudentScore(studentScore);
        Map<String, String> response = new HashMap<>();
        response.put("message", result ? "Insert success" : "Insert failed");
        return response;
//        return result ? "添加成功" : "添加失败";
    }



    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        studentScoreService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        studentScoreService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody StudentScore studentScore) {
        studentScoreService.updateById(studentScore);
        return Result.success();
    }



    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "10") int pageSize,
                             @RequestParam(required = false) String name,
                             @RequestParam(required = false) String courseName) {
        IPage<StudentScore> page = studentScoreService.selectPage(pageNum, pageSize, name, courseName);
        return Result.success(page);
    }

//    @GetMapping("/getAll")
//    public IPage<StudentScore> getAllStudentScores(
//            @RequestParam(defaultValue = "1") int page, // 默认页码是 1
//            @RequestParam(defaultValue = "10") int size // 默认每页大小是 10
//    ) {
//        return studentScoreService.getAllStudentScores(page, size);
//    }

    @GetMapping("/all")
    public IPage<StudentScore> getScores(@RequestParam(defaultValue = "1") int pageNum,
                                         @RequestParam(defaultValue = "5") int pageSize) {
        return studentScoreService.getScoresWithPagination(pageNum, pageSize);
    }

    @GetMapping("/progressAndScores")
    public ResponseEntity<List<StudentScore>> getProgressAndScores(HttpServletRequest request) {
        // 从前端传递的请求头中获取学生姓名
        String studentName = request.getHeader("studentName");
        if (studentName == null || studentName.trim().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        List<StudentScore> scores = studentScoreService.getStudentProgressAndScoresByName(studentName);
        if (scores.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(scores);
    }

}
